package com.lenovo.manufacture;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class BuyMaterialsActivity extends AppCompatActivity {

    private TextView fundText;
    private EditText posEdit;
    private EditText materialsidEdit;
    private Button buyBtn;
    private Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("http://hh1.me:5001/Interface/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    private Call<FactoryData> f;
    private Network network = retrofit.create(Network.class);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buy_materials);
        initView();
        getDataInfo(1);
    }

    private void getDataInfo(int x) {
        Map<String,Object> map=new HashMap<>();
        map.put("id",x);
        Call<FactoryData> factoryInfo = network.getFactoryInfo(map);
        factoryInfo.enqueue(new Callback<FactoryData>() {
            @Override
            public void onResponse(Call<FactoryData> call, Response<FactoryData> response) {
                fundText.setText("工厂资金："+response.body().getData().get(0).getGold());
            }

            @Override
            public void onFailure(Call<FactoryData> call, Throwable t) {
                Log.e("TAG", "onFailure: " + t.getMessage());
            }
        });


    }

    private void initView() {
        fundText = (TextView) findViewById(R.id.fund_text);
        posEdit = (EditText) findViewById(R.id.pos_edit);
        materialsidEdit = (EditText) findViewById(R.id.materialsid_edit);
        buyBtn = (Button) findViewById(R.id.buy_btn);
        buyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (posEdit.getText().toString()==null&&materialsidEdit.getText().toString()==null){
                    Toast.makeText(BuyMaterialsActivity.this, "请输入位置或原料ID", Toast.LENGTH_SHORT).show();
                }
                Call<SupplyListData> supplyListDataCall = network.supplyList();
                supplyListDataCall.enqueue(new Callback<SupplyListData>() {
                    @Override
                    public void onResponse(Call<SupplyListData> call, Response<SupplyListData> response) {
                        for (int i=0;i<response.body().getData().size();i++){
                            if (Integer.valueOf(materialsidEdit.getText().toString()).equals(response.body().getData().get(i).getId())){
                                Call<addMaterials> addMaterialsCall = network.getaddMaterials(Integer.valueOf(posEdit.getText().toString()),response.body().getData().get(i).getNum(),Integer.valueOf(materialsidEdit.getText().toString()));
                                addMaterialsCall.enqueue(new Callback<addMaterials>() {
                                    @Override
                                    public void onResponse(Call<addMaterials> call, Response<addMaterials> response) {
                                        Toast.makeText(BuyMaterialsActivity.this, "充值成功", Toast.LENGTH_SHORT).show();
                                            getDataInfo(1);
                                    }

                                    @Override
                                    public void onFailure(Call<addMaterials> call, Throwable t) {

                                    }
                                });
                            }
                        }

                    }

                    @Override
                    public void onFailure(Call<SupplyListData> call, Throwable t) {

                    }
                });
            }
        });
    }
}